
#include "../ui.h"
#include "../app_includes.h"

#define DP_TAB_NUM      (2)
#define DP_U_COLOR      (0xC86464)
#define DP_I_COLOR      (0x72A4A7)

static uint8_t tab_id = 0;

static lv_obj_t *ui_screen;
static lv_obj_t *ui_tabview;
static lv_obj_t *ui_tab[DP_TAB_NUM];
static lv_timer_t *lv_timer;

extern uint8_t power_used_num;
extern void cc_tab2_screen_create(lv_obj_t *tab);
static void dp_tab0_screen_create(lv_obj_t *tab);
static void dp_tab1_screen_create(lv_obj_t *tab);
static void dp_tab0_refresh(lv_timer_t *timer);
static void set_value_event_cb(lv_event_t * e);
static void power_select_refresh(void);
static void short_key_refresh(lv_obj_t *tab);
static void select_shortkey_event_cb(lv_event_t * e);
static void select_shortkey_back_event_cb(lv_event_t * e);

void lv_dp_close(void)
{
    lv_timer_del(lv_timer);
    if(app_dev.para.store.pd_close_auto && 
       app_dev.para.status.power_work==0 &&
       PD_protocol_set_power_option(&app_pt.protocol, PD_POWER_OPTION_MAX_5V)) 
    { 
        xEventGroupSetBits(pd_event, kPDEvent_PD_OUT);
    }
    
}

//app应用入口 
lv_obj_t *lv_dp_open(lv_obj_t *screen)
{
    ui_tabview = lv_tab_screen_create(screen, ui_tab, DP_TAB_NUM);
    dp_tab0_screen_create(ui_tab[0]);
    //dp_tab1_screen_create(ui_tab[1]);   
    cc_tab2_screen_create(ui_tab[1]);
    
    lv_timer = lv_timer_create(dp_tab0_refresh, 300, NULL);
    dp_group_select(0);
    
    if(app_dev.para.store.pd_open_auto && 
       app_dev.para.status.power_work==0 &&
       PD_protocol_set_power_option(&app_pt.protocol, PD_POWER_OPTION_MAX_POWER)) 
    { 
        msg_show_auto_close(LV_SYMBOL_WARNING, "已开启PD最大功率!!", 1500);//弹出已选择自动打开pd协议提示
        xEventGroupSetBits(pd_event, kPDEvent_PD_OUT);
    }
    
    return ui_tabview;
}

static lv_obj_t *ui_btn_shortkey;
static lv_obj_t *ui_LabelShortkey;
static lv_obj_t *ui_BtnOn;
static lv_obj_t *ui_LabelOn;
static lv_obj_t *ui_Panel1;
static lv_obj_t *ui_LabelDPVVal;
static lv_obj_t *ui_LabelDPIVal;
static lv_obj_t *ui_LabelDPPVal;
static lv_obj_t *ui_LabelDPVUnit;
static lv_obj_t *ui_LabelDPIUnit;
static lv_obj_t *ui_LabelDPPUnit;
static lv_obj_t *ui_PanelUSet;
static lv_obj_t *ui_EditUSet;
static lv_obj_t *ui_BarUSet;
static lv_obj_t *ui_BarISet;
static lv_obj_t *ui_PanelISet;
static lv_obj_t *ui_EditISet;
static lv_obj_t *ui_PanelPowerIn;
static lv_obj_t *ui_LabelPDType;
static lv_obj_t *ui_LabelPInVal;
static lv_obj_t *ui_BtnCVMode;
static lv_obj_t *ui_LabelCVMode;
static lv_obj_t *ui_PanelShortKey;
static lv_obj_t *ui_BtnGroup[DEVICE_SHORTKEY_NUM+1];

static void dp_tab0_screen_create(lv_obj_t *tab)
{
    ui_BtnOn = lv_btn_create(tab);
    lv_obj_set_width( ui_BtnOn, 50);
    lv_obj_set_height( ui_BtnOn, 30);
    lv_obj_align( ui_BtnOn, LV_ALIGN_BOTTOM_LEFT, 0, 0);
    lv_obj_add_flag( ui_BtnOn, LV_OBJ_FLAG_CHECKABLE | LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
    lv_obj_clear_flag( ui_BtnOn, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_BtnOn, lv_color_hex(0x292831), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_BtnOn, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_BtnOn, lv_color_hex(0x00FF00), LV_PART_MAIN | LV_STATE_CHECKED );
    lv_obj_set_style_bg_opa(ui_BtnOn, 255, LV_PART_MAIN| LV_STATE_CHECKED);

    ui_LabelOn = lv_label_create(ui_BtnOn);
    lv_obj_set_width( ui_LabelOn, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelOn, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_align( ui_LabelOn, LV_ALIGN_CENTER );
    lv_label_set_text(ui_LabelOn,"ON");
    lv_obj_set_style_text_color(ui_LabelOn, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelOn, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelOn, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_LabelOn, 10, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_BtnCVMode = lv_obj_create(tab);
    lv_obj_set_width( ui_BtnCVMode, 50);
    lv_obj_set_height( ui_BtnCVMode, 30);
    lv_obj_align_to( ui_BtnCVMode, ui_BtnOn, LV_ALIGN_OUT_RIGHT_TOP, 8, 0 );
    lv_obj_clear_flag( ui_BtnCVMode, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_BtnCVMode, lv_color_hex(0x72A4A7), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_BtnCVMode, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_BtnCVMode, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelCVMode = lv_label_create(ui_BtnCVMode);
    lv_obj_set_width( ui_LabelCVMode, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelCVMode, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_align( ui_LabelCVMode, LV_ALIGN_CENTER );
    lv_label_set_text(ui_LabelCVMode,"CC");
    lv_obj_set_style_text_font(ui_LabelCVMode, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_LabelCVMode, 10, LV_PART_MAIN| LV_STATE_DEFAULT);


    ui_btn_shortkey = lv_btn_create(tab);
    lv_obj_set_width( ui_btn_shortkey, 108);
    lv_obj_set_height( ui_btn_shortkey, 30);
    lv_obj_align_to( ui_btn_shortkey, ui_BtnCVMode, LV_ALIGN_OUT_RIGHT_TOP, 8, 0 );
    lv_obj_clear_flag( ui_btn_shortkey, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_btn_shortkey, lv_color_hex(0x2F4F2F), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(ui_btn_shortkey, lv_color_hex(0x41D080), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_border_width(ui_btn_shortkey, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelShortkey = lv_label_create(ui_btn_shortkey);
    lv_obj_set_width( ui_LabelShortkey, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelShortkey, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_align( ui_LabelShortkey, LV_ALIGN_CENTER );
    lv_label_set_text(ui_LabelShortkey,"快速设置");
    lv_obj_set_style_text_color(ui_LabelShortkey, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelShortkey, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelShortkey, &ui_font_FontChineseSmall, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_LabelShortkey, 10, LV_PART_MAIN| LV_STATE_DEFAULT);


    ui_Panel1 = lv_obj_create(tab);
    lv_obj_set_width( ui_Panel1, 240);
    lv_obj_set_height( ui_Panel1, 150);
    lv_obj_align( ui_Panel1, LV_ALIGN_CENTER, 0, -5);
    lv_obj_clear_flag( ui_Panel1, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_Panel1, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_Panel1, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_Panel1, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_Panel1, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelDPVVal = lv_label_create(ui_Panel1);
    lv_obj_set_width( ui_LabelDPVVal, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelDPVVal, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_x( ui_LabelDPVVal, -54 );
    lv_obj_set_y( ui_LabelDPVVal, -55 );
    lv_obj_set_align( ui_LabelDPVVal, LV_ALIGN_CENTER );
    lv_label_set_text(ui_LabelDPVVal,"20.00");
    lv_obj_set_style_text_color(ui_LabelDPVVal, lv_color_hex(0xC86464), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelDPVVal, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelDPVVal, &ui_font_FontNumberBig, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelDPIVal = lv_label_create(ui_Panel1);
    lv_obj_set_width( ui_LabelDPIVal, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelDPIVal, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_x( ui_LabelDPIVal, -54 );
    lv_obj_set_y( ui_LabelDPIVal, -5 );
    lv_obj_set_align( ui_LabelDPIVal, LV_ALIGN_CENTER );
    lv_label_set_text(ui_LabelDPIVal,"5.001");
    lv_obj_set_style_text_color(ui_LabelDPIVal, lv_color_hex(0x72A4A7), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelDPIVal, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelDPIVal, &ui_font_FontNumberBig, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelDPPVal = lv_label_create(ui_Panel1);
    lv_obj_set_width( ui_LabelDPPVal, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelDPPVal, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_x( ui_LabelDPPVal, -55 );
    lv_obj_set_y( ui_LabelDPPVal, 47 );
    lv_obj_set_align( ui_LabelDPPVal, LV_ALIGN_CENTER );
    lv_label_set_text(ui_LabelDPPVal,"100.0");
    lv_obj_set_style_text_color(ui_LabelDPPVal, lv_color_hex(0x60A3E7), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelDPPVal, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelDPPVal, &ui_font_FontNumberBig, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelDPVUnit = lv_label_create(ui_Panel1);
    lv_obj_set_width( ui_LabelDPVUnit, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelDPVUnit, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_x( ui_LabelDPVUnit, 119 );
    lv_obj_set_y( ui_LabelDPVUnit, -49 );
    lv_obj_set_align( ui_LabelDPVUnit, LV_ALIGN_LEFT_MID );
    lv_label_set_text(ui_LabelDPVUnit,"V");
    lv_obj_set_style_text_color(ui_LabelDPVUnit, lv_color_hex(0xC86464), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelDPVUnit, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelDPVUnit, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelDPIUnit = lv_label_create(ui_Panel1);
    lv_obj_set_width( ui_LabelDPIUnit, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelDPIUnit, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_x( ui_LabelDPIUnit, 120 );
    lv_obj_set_y( ui_LabelDPIUnit, 1 );
    lv_obj_set_align( ui_LabelDPIUnit, LV_ALIGN_LEFT_MID );
    lv_label_set_text(ui_LabelDPIUnit,"A");
    lv_obj_set_style_text_color(ui_LabelDPIUnit, lv_color_hex(0x72A4A7), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelDPIUnit, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelDPIUnit, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelDPPUnit = lv_label_create(ui_Panel1);
    lv_obj_set_width( ui_LabelDPPUnit, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelDPPUnit, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_x( ui_LabelDPPUnit, 117 );
    lv_obj_set_y( ui_LabelDPPUnit, 56 );
    lv_obj_set_align( ui_LabelDPPUnit, LV_ALIGN_LEFT_MID );
    lv_label_set_text(ui_LabelDPPUnit,"W");
    lv_obj_set_style_text_color(ui_LabelDPPUnit, lv_color_hex(0x60A3E7), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelDPPUnit, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelDPPUnit, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_BarUSet = lv_bar_create(ui_Panel1);
    lv_bar_set_range(ui_BarUSet, 0, 36000);
    lv_bar_set_value(ui_BarUSet,25,LV_ANIM_OFF);
    lv_obj_set_width( ui_BarUSet, 80);
    lv_obj_set_height( ui_BarUSet, 6);
    lv_obj_set_x( ui_BarUSet, 80 );
    lv_obj_set_y( ui_BarUSet, -70 );
    lv_obj_set_align( ui_BarUSet, LV_ALIGN_CENTER );
    lv_obj_set_style_radius(ui_BarUSet, 5, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_BarUSet, 5, LV_PART_INDICATOR| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_BarUSet, lv_color_hex(0xC86464), LV_PART_INDICATOR | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_BarUSet, 255, LV_PART_INDICATOR| LV_STATE_DEFAULT);

    ui_PanelUSet = lv_obj_create(ui_Panel1);
    lv_obj_set_width( ui_PanelUSet, 80);
    lv_obj_set_height( ui_PanelUSet, 30);
    lv_obj_align_to( ui_PanelUSet, ui_BarUSet, LV_ALIGN_OUT_BOTTOM_LEFT ,0, 4);
    lv_obj_clear_flag( ui_PanelUSet, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_PanelUSet, 5, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_PanelUSet, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelUSet, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_PanelUSet, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_EditUSet = lv_spinbox_create(ui_PanelUSet);
    lv_obj_set_width( ui_EditUSet, 80);
    lv_obj_set_height( ui_EditUSet, 60);
    lv_obj_align( ui_EditUSet, LV_ALIGN_TOP_LEFT, -8, -18);
    lv_spinbox_set_digit_format( ui_EditUSet, 4, 2);
    lv_spinbox_set_range( ui_EditUSet, 0,3600 );
    lv_spinbox_set_step( ui_EditUSet, 1);
    lv_spinbox_set_value( ui_EditUSet, 500 );
    lv_obj_set_style_bg_opa(ui_EditUSet, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_EditUSet, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_EditUSet, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_EditUSet, 0, LV_PART_CURSOR| LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_EditUSet, LV_TEXT_ALIGN_CENTER, LV_PART_CURSOR| LV_STATE_DEFAULT);
    lv_obj_set_style_text_decor(ui_EditUSet, LV_TEXT_DECOR_UNDERLINE, LV_PART_CURSOR| LV_STATE_DEFAULT);


    ui_BarISet = lv_bar_create(ui_Panel1);
    lv_bar_set_range(ui_BarISet, 0, 8000);
    lv_bar_set_value(ui_BarISet,25,LV_ANIM_OFF);
    lv_obj_set_width( ui_BarISet, 80);
    lv_obj_set_height( ui_BarISet, 6);
    lv_obj_align_to( ui_BarISet, ui_PanelUSet, LV_ALIGN_OUT_BOTTOM_LEFT ,0, 10);
    lv_obj_set_style_radius(ui_BarISet, 5, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_BarISet, 5, LV_PART_INDICATOR| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_BarISet, lv_color_hex(0x72A4A7), LV_PART_INDICATOR | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_BarISet, 255, LV_PART_INDICATOR| LV_STATE_DEFAULT);

    ui_PanelISet = lv_obj_create(ui_Panel1);
    lv_obj_set_width( ui_PanelISet, 80);
    lv_obj_set_height( ui_PanelISet, 30);
    lv_obj_align_to( ui_PanelISet, ui_BarISet, LV_ALIGN_OUT_BOTTOM_LEFT ,0, 4);
    lv_obj_clear_flag( ui_PanelISet, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_PanelISet, 5, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_PanelISet, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelISet, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_PanelISet, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_EditISet = lv_spinbox_create(ui_PanelISet);
    lv_obj_set_width( ui_EditISet, 80);
    lv_obj_set_height( ui_EditISet, 60);
    lv_obj_align( ui_EditISet, LV_ALIGN_TOP_LEFT, -8, -18);
    lv_spinbox_set_digit_format( ui_EditISet, 4, 1);
    lv_spinbox_set_range( ui_EditISet, 0,8000 );
    lv_spinbox_set_step( ui_EditISet, 1);
    lv_spinbox_set_value( ui_EditISet, 2000 );
    lv_obj_set_style_bg_opa(ui_EditISet, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_EditISet, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_EditISet, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_clear_flag(ui_EditISet,LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_opa(ui_EditISet, 0, LV_PART_CURSOR| LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_EditISet, LV_TEXT_ALIGN_CENTER, LV_PART_CURSOR| LV_STATE_DEFAULT);
    lv_obj_set_style_text_decor(ui_EditISet, LV_TEXT_DECOR_UNDERLINE, LV_PART_CURSOR| LV_STATE_DEFAULT);

    ui_PanelPowerIn = lv_obj_create(ui_Panel1);
    lv_obj_set_width( ui_PanelPowerIn, 80);
    lv_obj_set_height( ui_PanelPowerIn, 40);
    lv_obj_align_to( ui_PanelPowerIn, ui_PanelISet, LV_ALIGN_OUT_BOTTOM_LEFT ,0, 10);
    lv_obj_clear_flag( ui_PanelPowerIn, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_PanelPowerIn, lv_color_hex(0x292831), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelPowerIn, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_PanelPowerIn, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelPDType = lv_label_create(ui_PanelPowerIn);
    lv_obj_set_width( ui_LabelPDType, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelPDType, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_x( ui_LabelPDType, 0 );
    lv_obj_set_y( ui_LabelPDType, -9 );
    lv_obj_set_align( ui_LabelPDType, LV_ALIGN_CENTER );
    lv_label_set_text(ui_LabelPDType,"20V4.5A");
    lv_obj_set_style_text_color(ui_LabelPDType, lv_color_hex(0xE7E945), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelPDType, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelPDType, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_LabelPDType, 10, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelPInVal = lv_label_create(ui_PanelPowerIn);
    lv_obj_set_width( ui_LabelPInVal, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelPInVal, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_x( ui_LabelPInVal, 1 );
    lv_obj_set_y( ui_LabelPInVal, 8 );
    lv_obj_set_align( ui_LabelPInVal, LV_ALIGN_CENTER );
    lv_label_set_text(ui_LabelPInVal,"120.0W");
    lv_obj_set_style_text_color(ui_LabelPInVal, lv_color_hex(0xE7E945), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelPInVal, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelPInVal, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_LabelPInVal, 10, LV_PART_MAIN| LV_STATE_DEFAULT);

    lv_obj_add_event_cb(ui_EditUSet, set_value_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_add_event_cb(ui_EditISet, set_value_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    
    lv_obj_add_event_cb(ui_btn_shortkey, select_shortkey_event_cb, LV_EVENT_CLICKED, NULL);
    
    xEventGroupSetBits(sys_event, kSysEvent_ParaChange|kSysEvent_UIParaChange); //更新值
    
    power_select_refresh();
    short_key_refresh(tab);
}

static void dp_tab1_screen_create(lv_obj_t *tab)
{
    
}

static void dp_tab0_refresh(lv_timer_t *timer)
{
    char str[16];
    
    float_abs_to_str(str, app_dev.meter.power_out_v.value , 2, 5, NULL);  
    lv_label_set_text(ui_LabelDPVVal,str);
    float_abs_to_str(str, app_dev.meter.power_out_i.value , 3, 5, NULL);  
    lv_label_set_text(ui_LabelDPIVal,str);
    float_abs_to_str(str, app_dev.meter.power_out_p.value , 1, 5, NULL);  
    lv_label_set_text(ui_LabelDPPVal,str);
    float_abs_to_str(str, app_dev.meter.power_p.value , 1, 5, "W");  
    lv_label_set_text(ui_LabelPInVal,str);
    
    
    //refresh para
    EventBits_t event = xEventGroupWaitBits(sys_event,kSysEvent_UIParaChange,pdTRUE,pdFALSE,0);
    if(event & kSysEvent_UIParaChange) {
        if(app_dev.para.status.power_work)
            lv_obj_add_state(ui_BtnOn, LV_STATE_CHECKED);
        else
            lv_obj_clear_state(ui_BtnOn, LV_STATE_CHECKED);
        
        if(app_dev.para.status.power_mode==0) { //CV mode 
            lv_obj_set_style_bg_color(ui_BtnCVMode, lv_color_hex(DP_U_COLOR), LV_PART_MAIN | LV_STATE_DEFAULT );
            lv_label_set_text(ui_LabelCVMode,"CV");
        }
        else {
            lv_obj_set_style_bg_color(ui_BtnCVMode, lv_color_hex(DP_I_COLOR), LV_PART_MAIN | LV_STATE_DEFAULT );
            lv_label_set_text(ui_LabelCVMode,"CC");
        }
        
        lv_bar_set_value(ui_BarUSet,app_dev.para.run.power_set_u, LV_ANIM_ON);
        lv_bar_set_value(ui_BarISet,app_dev.para.run.power_set_i, LV_ANIM_ON);
        
        
        lv_obj_remove_event_cb(ui_EditUSet, set_value_event_cb);
        lv_obj_remove_event_cb(ui_EditISet, set_value_event_cb);
        
        lv_spinbox_set_value(ui_EditISet, app_dev.para.run.power_set_i);
        lv_spinbox_set_value(ui_EditUSet, app_dev.para.run.power_set_u/10);
        
        lv_obj_add_event_cb(ui_EditUSet, set_value_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
        lv_obj_add_event_cb(ui_EditISet, set_value_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    }
}

static void set_value_event_cb(lv_event_t * e)
{
    app_dev.para.run.power_set_u = lv_spinbox_get_value(ui_EditUSet)*10;
    app_dev.para.run.power_set_i = lv_spinbox_get_value(ui_EditISet);
    xEventGroupSetBits(sys_event, kSysEvent_ParaChange|kSysEvent_UIParaChange);
    app_dev_para_init();
}


static void power_select_refresh(void)
{
    char str[16];
    uint_to_str(str, app_pt.ready_voltage/20, 0, 3, "V");
    uint_to_str(str+strlen(str), app_pt.ready_current/10, 1, 4, "A");
    lv_label_set_text(ui_LabelPDType, str);
}

static void short_key_refresh(lv_obj_t *tab)
{
    uint8_t i;
    static lv_obj_t *ui_label;
    char str[16];
    
    ui_PanelShortKey = lv_obj_create(tab);
    lv_obj_set_width(ui_PanelShortKey, 140);
    lv_obj_set_height(ui_PanelShortKey, 190);
    lv_obj_align(ui_PanelShortKey, LV_ALIGN_LEFT_MID, -160, 10);
    lv_obj_set_style_bg_opa(ui_PanelShortKey, 220, LV_PART_MAIN| LV_STATE_DEFAULT);
    /*lv_obj_set_style_shadow_color(ui_PanelShortKey, lv_color_hex(0x72A4A7), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_opa(ui_PanelShortKey, 200, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_width(ui_PanelShortKey, 10, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_spread(ui_PanelShortKey, 4, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_ofs_x(ui_PanelShortKey, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_ofs_y(ui_PanelShortKey, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    */
    lv_obj_set_flex_flow(ui_PanelShortKey,LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(ui_PanelShortKey, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_row(ui_PanelShortKey, 6, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_add_flag( ui_PanelShortKey, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_scrollbar_mode(ui_PanelShortKey, LV_SCROLLBAR_MODE_OFF);
    
    for(i=0;i!=DEVICE_SHORTKEY_NUM+1;i++) {
        ui_BtnGroup[i] = lv_btn_create(ui_PanelShortKey);
        lv_obj_set_width( ui_BtnGroup[i], 120);
        lv_obj_set_height( ui_BtnGroup[i], 25);
        lv_obj_add_flag( ui_BtnGroup[i], LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
        lv_obj_clear_flag( ui_BtnGroup[i], LV_OBJ_FLAG_SCROLLABLE );    /// Flags
        lv_obj_set_style_bg_color(ui_BtnGroup[i], lv_color_hex(0x2F4F2F), LV_PART_MAIN | LV_STATE_DEFAULT );
        lv_obj_set_style_bg_color(ui_BtnGroup[i], lv_color_hex(0xF64031), LV_PART_MAIN | LV_STATE_FOCUSED );
        //lv_obj_set_style_outline_width(ui_BtnGroup[i], 0, LV_PART_MAIN| LV_STATE_FOCUSED);
        
        ui_label = lv_label_create(ui_BtnGroup[i]);
        lv_obj_set_width( ui_label, LV_SIZE_CONTENT);  /// 1
        lv_obj_set_height( ui_label, LV_SIZE_CONTENT);   /// 1
        lv_obj_set_align( ui_label, LV_ALIGN_CENTER );
        lv_obj_set_style_text_color(ui_label, lv_color_hex(0xffffff), LV_PART_MAIN | LV_STATE_DEFAULT );
        lv_obj_set_style_text_opa(ui_label, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
        lv_obj_set_style_text_font(ui_label, &ui_font_FontChineseSmall, LV_PART_MAIN| LV_STATE_DEFAULT);
        if(i==DEVICE_SHORTKEY_NUM)
            lv_label_set_text(ui_label,"返回");
        else {
            uint_to_str(str, app_dev.para.store.shortKey[i][0]/10, 2, 3, "V ");
            uint_to_str(str+strlen(str), app_dev.para.store.shortKey[i][1]/1, 3, 4, "A");
            lv_label_set_text(ui_label, str);
        }
        
        lv_obj_add_event_cb(ui_BtnGroup[i], select_shortkey_back_event_cb, LV_EVENT_CLICKED, NULL);
    }
}

static void select_shortkey_event_cb(lv_event_t * e)
{
    uint8_t i;
    move_x_Animation(ui_PanelShortKey, -160, 0);
    
    ui_main_screen.btn_disable = 1; //先禁止右键
    lv_group_set_editing(g_encoder, 0);
    lv_group_remove_all_objs(g_encoder);
    for(i=0;i!=DEVICE_SHORTKEY_NUM+1;i++) {
        lv_group_add_obj(g_encoder, ui_BtnGroup[i]);
    }
    
}

static void select_shortkey_back_event_cb(lv_event_t * e)
{
    uint32_t index;
    lv_group_remove_all_objs(g_encoder);
    dp_group_select(0);
    move_x_Animation(ui_PanelShortKey, 0, -160);
    //select group
    
    index = lv_obj_get_child_id(e->target);
    if(index<DEVICE_SHORTKEY_NUM) {
        app_dev.para.run.power_set_u = app_dev.para.store.shortKey[index][0];
        app_dev.para.run.power_set_i = app_dev.para.store.shortKey[index][1];
        xEventGroupSetBits(sys_event, kSysEvent_ParaChange|kSysEvent_UIParaChange);
        app_dev_para_init();  
    }
    
    ui_main_screen.btn_disable = 0; //恢复右键
        
}

void dp_group_select(uint8_t id)
{
    uint8_t i;
    lv_timer_pause(lv_timer);
    switch(id){
    case 0:  
        lv_group_add_obj(g_encoder, ui_btn_shortkey);
        lv_group_add_obj(g_encoder, ui_EditUSet);
        lv_group_add_obj(g_encoder, ui_EditISet);
        lv_timer_set_cb(lv_timer, dp_tab0_refresh);
        break;
    case 1:
        cc_group_select(3);
        break;
    default:
        lv_timer_set_cb(lv_timer, NULL);
        break;
    }
    lv_timer_resume(lv_timer);
}
